//
//  FindPwdViewController2.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FindPwdViewController.h"
//view
#import "FDSameToAndroidTextFieldCustomView.h"

//controller
#import "LoginAndRegisterController.h"


@interface FindPwdViewController ()
/**提示labe*/
@property (nonatomic, strong) UILabel *tipsLb;
/**输入框按钮*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *pwdTFView;
/**完成登录按钮*/
@property (nonatomic, strong) UIButton *loginBtn;
@end

@implementation FindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"找回密码";
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tipsLb];
    [self.view addSubview:self.pwdTFView];
    [self.view addSubview:self.loginBtn];
    
    [self.tipsLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(180);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    [self.pwdTFView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipsLb.mas_bottom).offset(30);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.pwdTFView.mas_bottom).offset(30);
    }];
    
//    //默认密文
//
//    //输入框线
//    UIView *lightGrayLineView = [UIView new];
//    lightGrayLineView.backgroundColor = kMainLightGrayColor;
//    [self.pwdTF addSubview:lightGrayLineView];
//    [lightGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.pwdTF);
//        make.height.mas_equalTo(1);
//        make.bottom.equalTo(self.pwdTF.mas_bottom).offset(-1);
//    }];
//
//    //输入框右边睁眼闭眼按钮
//    UIButton *openCloseEyeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    [openCloseEyeButton setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
//    [openCloseEyeButton setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
//    openCloseEyeButton.frame = CGRectMake(0, 0, 30, 20);
//    self.pwdTF.rightView = openCloseEyeButton;
//    self.pwdTF.rightViewMode = UITextFieldViewModeAlways;
//
//    [openCloseEyeButton addTarget:self action:@selector(clickOpenCloseEyeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - getter
- (UILabel *)tipsLb {
    if (!_tipsLb) {
        _tipsLb = [UILabel new];
        _tipsLb.font = [UIFont systemFontOfSize:15.0];
        _tipsLb.text = [NSString stringWithFormat:@"为%@设置新的登录密码",self.email];
        _tipsLb.textAlignment = NSTextAlignmentCenter;
    }
    return _tipsLb;
}
- (FDSameToAndroidTextFieldCustomView *)pwdTFView {
    if (!_pwdTFView) {
        
        NSString *qsrxmmStr = NSLocalizedString(Put_your_newPWd, @"请输入新密码");
        _pwdTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:qsrxmmStr];
        _pwdTFView.tf.secureTextEntry = YES; //默认显示密文
        
        //右边眼睛按钮
        //输入框右边睁眼闭眼按钮
        UIButton *openCloseEyeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [openCloseEyeButton setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
        [openCloseEyeButton setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
        [openCloseEyeButton addTarget:self action:@selector(clickOpenCloseEyeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_pwdTFView.topBgView addSubview:openCloseEyeButton];
        [openCloseEyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_pwdTFView.topBgView);
            make.width.mas_equalTo(30);
        }];
    }
    return _pwdTFView;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        NSString *loginStr = NSLocalizedString(ComplectionAndLogin, @"完成并登录");
        [_loginBtn setTitle:loginStr forState:(UIControlStateNormal)];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_loginBtn addTarget:self action:@selector(clickComplectionAndLoginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _loginBtn.backgroundColor = kMainRedColor;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.cornerRadius = 4; //圆角弧度
        
    }
    return _loginBtn;
}

#pragma mark - 点击事件
//睁眼闭眼
- (void)clickOpenCloseEyeBtnAction:(UIButton *)button {
    button.selected = !button.selected;
    
    if (button.selected) {
        self.pwdTFView.tf.secureTextEntry = NO;
    }else {
        self.pwdTFView.tf.secureTextEntry = YES;
    }
}

//完成并登陆
- (void)clickComplectionAndLoginButtonAction:(UIButton *)button {
    [self.view endEditing:YES];
    
    NSString *pwdStr = self.pwdTFView.tf.text;
    NSString *toastStr = NSLocalizedString(Put_your_newPWd, @"请输入新密码");
    
    if ([NSString isEmptyString:pwdStr]) {
        [JRToast showWithText:toastStr];
        return;
    }
    [self requestChangePwd:pwdStr];
}


- (void)requestChangePwd:(NSString *)pwd {
    NSDictionary *parDic = @{@"password":pwd,
                             @"email":self.email,
                             @"code":self.code,
                             @"token":self.token
                             };
    [FDHttpAPi requestChangePwdPardic:parDic successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            [self changeVC]; //切换控制器
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}

- (void)changeVC {
    //消失
    [self dismissViewControllerAnimated:NO completion:nil];
    
    LoginAndRegisterController *loginAndRegisterVC = [[LoginAndRegisterController alloc] initWithNibName:@"LoginAndRegisterController" bundle:nil];
    
    //切换控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = loginAndRegisterVC;
}

#pragma mark - 回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.pwdTFView.tf resignFirstResponder];
}
@end
